import urllib.request
import urllib.parse
import json

# post请求

base_url = "https://fanyi.baidu.com/sug"
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36'
}
data = {
    "kw":"spider"
}
# post请求的参数，必须要进行编码
data = urllib.parse.urlencode(data).encode("utf8")
# post请求的参数，需要放在请求对象定制的参数中
# post请求的参数，必须进行编码
request = urllib.request.Request(url=base_url, data=data, headers=headers)
# 模拟浏览器发送请求
response = urllib.request.urlopen(request)
content = response.read().decode("utf8")
print(content)
print(type(content))

# 把字符串转换为json对象
obj = json.loads(content)
print(obj)
print(type(obj))